package com.ruoyi.hqfinance.service.impl;

import com.ruoyi.hqfinance.domain.HqClientRelation;
import com.ruoyi.hqfinance.domain.dto.HqClientRelationDto;
import com.ruoyi.hqfinance.domain.vo.HqClientRelationVo;
import com.ruoyi.hqfinance.mapper.HqClientRelationDtoMapper;
import com.ruoyi.hqfinance.mapper.HqClientRelationMapper;
import com.ruoyi.hqfinance.service.HqClientRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HqClientRelationServiceImpl implements HqClientRelationService {
    @Autowired
    private HqClientRelationMapper hqClientRelationMapper;
    @Autowired
    private HqClientRelationDtoMapper hqClientRelationDtoMapper;

    @Override
    public List<HqClientRelation> selectAll() {
        List<HqClientRelation> list = hqClientRelationMapper.selectAll();
        for (HqClientRelation hqClientRelation : list) {
            hqClientRelation.setClientRelations(hqClientRelationMapper.selectSmallClient(hqClientRelation.getId()));
        }
        return list;
    }

    @Override
    public Boolean update(HqClientRelation hqClientRelation) {
        return hqClientRelationMapper.update(hqClientRelation);
    }

    @Override
    public Boolean insertBigClient(HqClientRelation hqClientRelation) {
        hqClientRelation.setLevel("父客户");
        hqClientRelation.setParentId(null);
        return hqClientRelationMapper.insert(hqClientRelation);
    }

    @Override
    public Boolean insertSmallClient(HqClientRelation hqClientRelation) {
        hqClientRelation.setLevel("子客户");
        hqClientRelation.setNetworkName(hqClientRelationMapper.findNetworkNameByParentId(hqClientRelation.getParentId()));
        hqClientRelation.setNetworkNameId(hqClientRelationMapper.findNetworkCodeByParentId(hqClientRelation.getParentId()));
        return hqClientRelationMapper.insert(hqClientRelation);
    }

    @Override
    public List<HqClientRelation> selectBigClient() {
        return hqClientRelationMapper.selectBigClient();
    }

    @Override
    public List<HqClientRelation> search(HqClientRelationVo vo) {
        List<HqClientRelation> list = hqClientRelationMapper.search(vo);
        for (HqClientRelation hqClientRelation : list) {
            hqClientRelation.setClientRelations(hqClientRelationMapper.selectSmallClient(hqClientRelation.getId()));
        }
        return list;
    }

    @Override
    public Boolean deleteById(Integer id) {
        String level = hqClientRelationMapper.findById(id).getLevel();
        List<Integer> ids = hqClientRelationMapper.selectIdsByParentId(id);
        if (ids == null || ids.size() == 0) {
            return hqClientRelationMapper.deleteById(id);
        }
        if (level.equals("父客户")) {
            for (Integer idSon : ids) {
                hqClientRelationMapper.deleteById(idSon);
            }
        }
        return hqClientRelationMapper.deleteById(id);

    }

    @Override
    public String findParentName(String name) {
        return hqClientRelationMapper.findParentName(name);
    }

    @Override
    public String findSonName(String name) {
        return hqClientRelationMapper.findSonName(name);
    }

    @Override
    public List<HqClientRelationDto> export() {
        return hqClientRelationDtoMapper.selectAll();
    }
}
